package com.example.kkblog.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.kkblog.config.ConstParam;
import com.example.kkblog.controller.dto.ResponseDto;
import com.example.kkblog.domain.DailyCheck;
import com.example.kkblog.domain.User;
import com.example.kkblog.domain.dto.UserDto;
import com.example.kkblog.mapper.DailyCheckMapper;
import com.example.kkblog.mapper.UserMapper;
import com.example.kkblog.service.KKBlogService;
import com.example.kkblog.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @Author xiaoke
 * @Date 2024 06 02 00 08
 **/
@Controller
@RequestMapping("/check")
public class DailyCheckController {

    @Autowired
    private DailyCheckMapper checkMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private KKBlogService kkBlogService;


    @GetMapping("/doCheck")
    @ResponseBody
    public ResponseDto check(HttpSession session) {
        UserDto loginUser = (UserDto) session.getAttribute("user");
        // 先检查用户有没有签到
        String nowDay = DateUtils.dateFormat(new Date(), "yyyy-MM-dd");
        QueryWrapper<DailyCheck> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("check_day", nowDay).eq("user_id", loginUser.getId());
        if (checkMapper.selectCount(queryWrapper) > 0) {
            return ResponseDto.Fail("您今天已经签过到了~");
        }
        // 新建签到对象
        DailyCheck dailyCheck = new DailyCheck();
        dailyCheck.setUserId(loginUser.getId());
        dailyCheck.setCheckDay(nowDay);
        // 查询用户昨天有没有签到
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("check_day", DateUtils.getCalculateDay(new Date(), -1)).eq("user_id", loginUser.getId());
        DailyCheck yesterdayCheck = checkMapper.selectOne(queryWrapper);
        if (yesterdayCheck != null){
            dailyCheck.setDays(yesterdayCheck.getDays() + 1);
            dailyCheck.setScores(yesterdayCheck.getScores() + ConstParam.CONTINUE_CHECK_SCORES);
        } else {
            dailyCheck.setDays(1);
            dailyCheck.setScores(ConstParam.FIRST_CHECK_SCORES);
        }
        checkMapper.insert(dailyCheck);
        // 更新用户积分
        User user = new User();
        user.setId(loginUser.getId());
        user.setScores(dailyCheck.getScores());
        userMapper.addUserScores(user);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("dailyCheck", dailyCheck);
        jsonObject.put("checkUsers", kkBlogService.selectCheckUserTop());
        return ResponseDto.Success("签到成功！", jsonObject);
    }
}
